//
// PieChart
//
// CreativeCommons Attribution-ShareAlike
// http://creativecommons.org/licenses/by-sa/2.5/
//
// 2011 Elisabeth Robson
//
// expects an object with one or more of these:
// includeLabels - boolean, determines whether to include the specified labels
//	when drawing the chart. If false, the labels are stored in the pie chart
//	but not drawn by default. You can draw a label for a segment with 
//	the drawLabel method.
// data - array of data items. Should be positive integer adding up to 360.
// labels - array of (string) labels. Should have at least as many items as data.
// colors - two D array of (string) colors. First is used to draw, second to draw
//	a selected segment.
//
  function blink() {
    var blinks = document.getElementsByTagName('blink');
    for (var i = blinks.length - 1; i >= 0; i--) {
      var s = blinks[i];
      s.style.visibility = (s.style.visibility === 'visible') ? 'hidden' : 'visible';
    }
    window.setTimeout(blink, 1000);
  }
  
  function enabledSelectionLetter()
  {
  	$('.char-letter').each(function(){
      	$(this).unbind('click').bind('click', function(){
      		getLetter($(this).text());
  		});
      });
  	
   }
	function disabledSelectionLetter()
	{
		$('.char-letter').each(function(){
      	$(this).unbind('click');
      });
	}
	
  function getLetter(letter){
    conn.send('letter%%C%%' + letter);
    disabledSelectionLetter();
  }
  // Remove letter which is displayed on the cell
  function removeLetterCorrected( sz_LetterCorrected ) {
	  $('.char-letter').each(function(){
	      if ( sz_LetterCorrected == $(this).text() )
	    	  $(this).parent().hide();
	  });
  }
//show letter which is displayed on the cell
  function showLetterCorrected( sz_LetterCorrected ) {
	  $('.char-letter').each(function(){
		  $(this).parent().show();
	  });
  }
  
  function letterReveal(sz_Position){
    var a_Elements = sz_Position.split("%%S%%");
    if (a_Elements[1]) {
        var a_Pos = a_Elements[1].split(",");
        for (var i = 0; i < a_Pos.length; i++) {
            $("#cell-" + a_Pos[i]).removeClass("cell active");
            $("#cell-" + a_Pos[i]).addClass("cell");
            $("#cell-" + a_Pos[i]).text(a_Elements[0]);
        }
        removeLetterCorrected( a_Elements[0] );
        $("#response").text("Have " + a_Pos.length + " " + a_Elements[0] + " letters!");
        if ($('#cells').find('.active').length < 1) {
            setEndRoundTime(10);
        }
    }
    else {
        $("#response").text("Do not have any " + a_Elements[0] + " letter!");
        setTurnTime(3);
    }
  }
  
//Enabled event buyvowel
  function enabledBuyVowel() {
	  disabledSelectionLetter();
      $('.letter-group').find('.active').each(function(){
          $(this).unbind('click').bind('click', function(){
              sCellId = $(this).attr('id');
              iPosition = sCellId.split('-')[1];
          	buyVowel( iPosition );
          });
      });
  }
  function disabledBuyVowel() {
  	$('.letter-group').find('.active').each(function(){
          $(this).unbind('click');
      });
	}
  
  function buyVowel(position){
    conn.send('vowel%%C%%' + position);
    disabledBuyVowel();
    //window.alert(position);
  }
  
  function vowelReveal(sz_Letter){
    var a_Elements = sz_Letter.split("%%S%%");
    if (a_Elements[1]) {
        $("#cell-" + a_Elements[0]).removeClass("cell active");
        $("#cell-" + a_Elements[0]).addClass("cell");
        $("#cell-" + a_Elements[0]).text(a_Elements[1]);
        $("#response").text("You got " + a_Elements[1] + " letter!");
        if ($('#cells').find('.active').length < 1) {
            setEndRoundTime(10);
        }
    }
    else {
        $("#response").text("Illigal position!");
    }
  }
  
  
  if (document.addEventListener) document.addEventListener("DOMContentLoaded", blink, false);
  else if (window.addEventListener) window.addEventListener("load", blink, false);
  else if (window.attachEvent) window.attachEvent("onload", blink);
  else window.onload = blink;
